﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using WebChatServer.Models;

namespace WebChatServer.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    [HttpHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")]
    public class StatusController : ApiController
    {
        IFriendRepository _repository = new FriendRepository();
        ChatRepository chatRepository = new ChatRepository();

        public MemberStatus Get(string id)
        {
            
            FriendStatus[] friendOnls = _repository.GetFriend(id);
            if (friendOnls != null)
            {
                Status[] status = chatRepository.CheckStatus();
                if (status != null)
                {
                    for (int i = 0; i < friendOnls.Count(); i++)
                    {
                        for (int j = 0; j < status.Count(); j++)
                        {
                            if (friendOnls[i].Id == status[j].Id)
                            {
                                friendOnls[i].Status = 1;
                            }
                        }
                    }
                    return new MemberStatus() {Friends = friendOnls};
                }
            }
            return null;
        }

        public string Post(string id)
        {
            chatRepository.DeleteStatus(id);
            return "success";
        }
    }
}
